<?php
/**
 * golangshop商城系统 - 团队十年电商经验汇集巨献!
 * =========================================================
 * Copy right 2019-2029 杭州瑞璞玉数字科技有限公司, 保留所有权利。
 * ----------------------------------------------
 * 官方网址: https://www.golangshop.com
 * =========================================================
 */

namespace addon\store\model\settlement;

use app\model\BaseModel;

use addon\store\model\Config;
use addon\store\model\StoreAccount;
use app\model\store\Store;

class OrderSettlement extends BaseModel
{

    /**
     * 门店订单分成计算
     * @param $data
     */
    public function orderSettlementAccount($data)
    {

        $order_id = $data[ 'order_id' ];
        $order_info = model('order')->getInfo([ [ 'order_id', '=', $order_id ] ]);
        if (empty($order_info)) return null;
        $store_id = $order_info[ 'store_id' ];
        $site_id = $order_info[ 'site_id' ];
        $config_model = new Config();
        $config = $config_model->getStoreWithdrawConfig($site_id)[ 'data' ][ 'value' ] ?? [];
        $is_settlement = $config[ 'is_settlement' ];//结算模式  todo 系统配置的门店计算开关是否会对门店结算开关造成影响
        if ($is_settlement > 0) {
            $store_model = new Store();
            $store_condition = array (
                [ 'site_id', '=', $site_id ],
                [ 'store_id', '=', $store_id ]
            );
            $store_info = $store_model->getStoreInfo($store_condition)[ 'data' ] ?? [];
            $is_settlement = $store_info[ 'is_settlement' ];
            if ($is_settlement > 0) {
                $settlement_pay_type = $config['settlement_pay_type'] ?? [];//参与结算的支付方式
                if(!empty($settlement_pay_type)){
                    if(in_array($order_info['pay_type'], explode(',', $settlement_pay_type))){
                        $store_settlement_rate = $store_info[ 'settlement_rate' ];
                        if ($store_settlement_rate > 0) {
                            $settlement_rate = $store_settlement_rate;
                        } else {
                            $settlement_rate = $config[ 'settlement_rate' ];
                        }

                        if ($settlement_rate > 0) {
                            $settlement_rate_calc = $settlement_rate / 100;//计算门店抽成比例
                            $order_money = $order_info[ 'order_money' ];
                            //todo  可能还有退款
                            $settlement_cost = $config[ 'settlement_cost' ];
                            $settlement_cost_array = explode(',', $settlement_cost);
                            $base_money = $order_money;
                            $base_money -= $order_info[ 'refund_money' ] ?? 0;//todo  这儿涉及到一个问题,退款的金额里面包含一部分余额,可能结算金额不太一致
                            $base_money = $base_money < 0 ? 0 : $base_money;
//                            if (in_array('balance', $settlement_cost_array)) {
                            //结算时不结算余额
                            $balance_money = $order_info[ 'balance_money' ];
                            $base_money -= $balance_money;
//                            }
                            if (in_array('point', $settlement_cost_array)) {
                                $point_money = $order_info[ 'point_money' ];
                                $base_money += $point_money;
                            }
                            if (in_array('fenxiao_commission', $settlement_cost_array)) {
                                $commission = $order_info[ 'commission' ];
                                $base_money -= $commission;
                            }
                            //优惠券比较特殊, 不过不扣除要价格优惠券抵扣金额加回去
                            if (!in_array('coupon', $settlement_cost_array)) {
                                $coupon_money = $order_info[ 'coupon_money' ];
                                $base_money += $coupon_money;
                            }

                            if ($base_money > 0) {
                                $store_commission_rate = $settlement_rate;
                                $store_commission = round($base_money * $settlement_rate_calc, 2);
                                $order_data = array (
                                    'store_commission_rate' => $store_commission_rate,
                                    'store_commission' => $store_commission
                                );
                                $order_condition = array (
                                    [ 'order_id', '=', $order_id ]
                                );
                                model('order')->update($order_data, $order_condition);

                                $store_account_model = new StoreAccount();
                                //门店账户金额增加
                                $store_account_data = array (
                                    'account_data' => $store_commission,
                                    'site_id' => $site_id,
                                    'store_id' => $store_id,
                                    'from_type' => 'order',
                                    'remark' => '订单完成,门店抽成金额' . $store_commission,
                                    'related_id' => $order_id
                                );
                                $store_account_model->addStoreAccount($store_account_data);
                            }

                        }
                    }

                }

            }
        }
    }

    /**
     *
     * @param $data
     * @return void
     */
    public function orderRefundSettlementAccount($data)
    {
        $order_goods_id = $data[ 'order_goods_id' ];
        $order_goods_info = model('order_goods')->getInfo([ [ 'order_goods_id', '=', $order_goods_id ] ]);
        if (empty($order_goods_info))
            return null;

        $order_id = $order_goods_info[ 'order_id' ];
        $order_info = model('order')->getInfo([ [ 'order_id', '=', $order_id ] ]);
        if (empty($order_info))
            return null;

        $site_id = $order_info[ 'site_id' ];
        $store_id = $order_info[ 'store_id' ];
        $order_scene = $order_info[ 'order_scene' ];
        //只有收银台订单会有这种情况
        if ($store_id == 0 || $order_scene != 'cashier')
            return null;

        $refund_money = $order_goods_info[ 'refund_real_money' ];
        $order_money = $order_info[ 'order_money' ];
        if($order_money <= 0)
            return null;
        $store_commission = $order_info[ 'store_commission' ];
        $refund_store_commission = round($store_commission * ( $refund_money / $order_money ), 2);
        $store_account_model = new StoreAccount();
        //门店账户金额增加
        $store_account_data = array (
            'account_data' => -$refund_store_commission,
            'site_id' => $site_id,
            'store_id' => $store_id,
            'from_type' => 'refund',
            'remark' => '订单退款,门店抽成金额扣除' . $refund_store_commission,
            'related_id' => $order_goods_id,
            'is_limit' => 0
        );
        $store_account_model->addStoreAccount($store_account_data);

    }

}